package com.stationbuilding.adapter.task;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.stationbuilding.R;
import com.stationbuilding.bean.QuestionBean;
import com.stationbuilding.bean.TaskBean;
import com.stationbuilding.constants.Constants;

import java.util.List;

public class TaskListAdapter extends BaseAdapter {
    private List<QuestionBean> list = null;
    private Context context;

    public TaskListAdapter(Context context, List<QuestionBean> list) {
        this.context = context;
        this.list = list;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<QuestionBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        QuestionBean questionBean = list.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.task_item, null);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.video = (ImageView) convertView.findViewById(R.id.video);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.initiator = (TextView) convertView.findViewById(R.id.initiator);
            viewHolder.updateDate = (TextView) convertView.findViewById(R.id.update_date);
            viewHolder.type = (TextView) convertView.findViewById(R.id.type);
            viewHolder.status = (TextView) convertView.findViewById(R.id.status);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (questionBean.getFileList() != null && questionBean.getFileList().size() > 0) {
            String fileType = questionBean.getFileList().get(0).getDocMold();
            if (Constants.IMAGE_TYPE_LIST.contains(fileType)) {
                Glide.with(context).load(Constants.BASE_URL + questionBean.getFileList().get(0).getDocPath().replaceAll("\\\\", "/")).into(viewHolder.image);
                viewHolder.video.setVisibility(View.GONE);
            } else if (Constants.VIDEO_TYPE_LIST.contains(fileType)) {
                String[] paths = questionBean.getFileList().get(0).getDocPath().split(",");
                Glide.with(context).load(Constants.BASE_URL + paths[0].replaceAll("\\\\", "/")).into(viewHolder.image);
                viewHolder.video.setVisibility(View.VISIBLE);
            } else {
                Glide.with(context).load(R.drawable.default_image).into(viewHolder.image);
                viewHolder.video.setVisibility(View.GONE);
            }
        } else {
            Glide.with(context).load(R.drawable.default_image).into(viewHolder.image);
            viewHolder.video.setVisibility(View.GONE);
        }

        viewHolder.title.setText(questionBean.getQuestionName());
        viewHolder.initiator.setText(questionBean.getInitiator());
        viewHolder.updateDate.setText(questionBean.getStartDate());
        if (questionBean.getSafeQualityFlag() == 1) {
            viewHolder.type.setText("质量");
        } else {
            viewHolder.type.setText("安全");
        }
        viewHolder.status.setText(questionBean.getProcessStatusName());
//        viewHolder.initiator.setText(taskBean.get);
//        viewHolder.project.setText(taskBean.getProjectName());
//        viewHolder.type.setText(taskBean.getType());
//        viewHolder.message.setText(taskBean.getHandleUserName() + taskBean.getHandleStatusName() + "\"" + taskBean.getQuestionName() + "\", 请查收");

        return convertView;
    }

    final static class ViewHolder {
        ImageView image;
        ImageView video;
        TextView title;
        TextView initiator;
        TextView updateDate;
        TextView type;
        TextView status;
    }
}